//
//  VersionUpdateView.m
//  dyseller
//
//  Created by 红喇叭 on 2017/9/4.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "VersionUpdateView.h"

@implementation VersionUpdateView

- (instancetype)initWithFrame:(CGRect)frame updateData:(NSArray *)data{
    self = [super initWithFrame:frame];
    if(self){
        _dataArray = data;
      
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [self updateContent];
    }
    return self;
}

- (void)updateContent{
    
    UIButton *shutdown_Btn = [[UIButton alloc] init];
    [shutdown_Btn setImage:[UIImage imageNamed:@"New_pop_btn2_default"] forState:UIControlStateNormal];
    [shutdown_Btn addTarget:self action:@selector(removeV) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:shutdown_Btn];
    shutdown_Btn.sd_layout.topSpaceToView(self, GETHEIGHT(40)).rightSpaceToView(self, GETHEIGHT(40)).widthIs(GETWITH(42)).heightEqualToWidth();
    
    if(_isMandatory==YES) shutdown_Btn.hidden = YES;
    
    UIView *big_View = [[UIView alloc] init];

    [self addSubview:big_View];
    big_View.sd_layout.widthIs(GETWITH(480)).heightIs(GETHEIGHT(800)).centerYEqualToView(self).centerXEqualToView(self);
    
    UIImageView *top_heder_IMG = [[UIImageView alloc] init];
    top_heder_IMG.image = [UIImage imageNamed:@"New_pop_bg1_default.png"];
    [big_View addSubview:top_heder_IMG];
    top_heder_IMG.sd_layout.topSpaceToView(big_View, 0).widthIs(GETWITH(480)).heightIs(GETHEIGHT(400)).leftSpaceToView(big_View, 0);
    
    UIImageView *bottom_footer_IMG = [[UIImageView alloc] init];
    bottom_footer_IMG.image = [UIImage imageNamed:@"New_pop_bg2_default.png"];
    [big_View addSubview:bottom_footer_IMG];
    bottom_footer_IMG.sd_layout.topSpaceToView(top_heder_IMG, 0).widthIs(GETWITH(480)).rightSpaceToView(big_View, 0).bottomSpaceToView(big_View, 0);
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor whiteColor];
    [big_View addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    //UITableView去掉自带系统的分割线
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.sd_layout.leftSpaceToView(big_View, GETWITH(20)).topSpaceToView(top_heder_IMG, GETHEIGHT(10)).rightSpaceToView(big_View, GETWITH(20)).heightIs(GETHEIGHT(300));
    
    UIButton *update_Btn = [[UIButton alloc] init];
    [update_Btn setBackgroundImage:[UIImage imageNamed:@"New_pop_btn1_default.png"] forState:UIControlStateNormal];
    [update_Btn setTitle:@"去更新" forState:UIControlStateNormal];
    update_Btn.titleLabel.textColor = UIColorFromRGB(0xffffff);
    update_Btn.titleLabel.font = font15;
    [update_Btn addTarget:self action:@selector(To_update:) forControlEvents:UIControlEventTouchUpInside];
    [big_View addSubview:update_Btn];
    update_Btn.sd_layout.topSpaceToView(tableView, GETHEIGHT(10)).centerXEqualToView(big_View).widthIs(GETWITH(200)).heightIs(GETHEIGHT(60));
    
    
}



- (void)removeV{
    [self removeFromSuperview];
}

- (void)To_update:(UIButton *)btn{
    UIApplication *application = [UIApplication sharedApplication];
    [application openURL:[NSURL URLWithString:_downloadAdress]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    
    UILabel *title = [[UILabel alloc] init];
    title.font = font17;
    title.textAlignment = NSTextAlignmentLeft;
    title.text = _dataArray[indexPath.row];
    [cell.contentView addSubview:title];
    title.sd_layout.leftSpaceToView(cell.contentView, GETWITH(20)).rightSpaceToView(cell.contentView, GETWITH(20)).autoHeightRatio(0).centerYEqualToView(cell.contentView);
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *goodsname = _dataArray[indexPath.row];
     CGSize titleSize = [goodsname boundingRectWithSize:CGSizeMake(self.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font17} context:nil].size;
    
    return titleSize.height + 15;
}


@end
